package com.hxkcloud.patient.activitys;

import android.content.Intent;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.internal.LinkedTreeMap;
import com.hxkcloud.patient.R;
import com.hxkcloud.patient.beans.resultbeans.ObjectResultBean;
import com.hxkcloud.patient.request.MessageCode;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;


/**
 * Created by ray_lei on 2016/7/18.
 * 心电图列表
 */
public class ReportListActivity extends BaseActivity implements View.OnClickListener{

    private static final String TAG = ReportListActivity.class.getSimpleName();
    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private LinearLayout ll_empty;
    private TextView tv_empty;

    private ObjectResultBean<LinkedHashMap<String, ArrayList<LinkedTreeMap<String,Object>>>> wholeBean;
    private MyExpendAdapter adapter;


    private ExpandableListView lv_ecg;


    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_REPORTLIST_SUCCESS:

                if (msg.obj != null) {
                    wholeBean = (ObjectResultBean<LinkedHashMap<String, ArrayList<LinkedTreeMap<String,Object>>>>) msg.obj;
                    LinkedHashMap<String, ArrayList<LinkedTreeMap<String,Object>>> data = wholeBean.getData();
                    adapter.setData(data);
                    adapter.notifyDataSetChanged();
                    for (int i = 0; i < data.keySet().size(); i++) {
                        lv_ecg.expandGroup(i);
                    }
                } else {
                    toastMsg(R.string.datagetfail);
                }

                if (wholeBean == null) {
                    ll_empty.setVisibility(View.VISIBLE);
                }
                break;
            case MessageCode.NETWORKCODE_REPORTLIST_FAILURE:

                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                } else {
                    toastMsg(R.string.datagetfail);
                }


                    ll_empty.setVisibility(View.VISIBLE);

                break;
            case MessageCode.NETWORKCODE_CONNECT_ERROR:

                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_ecglist);
        initView();
        initListener();
        initData();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);
        lv_ecg = (ExpandableListView) findViewById(R.id.lv_ecg);
        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText("心电监测");
        iv_back.setOnClickListener(this);


        adapter = new MyExpendAdapter();
        lv_ecg.setAdapter(adapter);
        lv_ecg.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                                        int groupPosition, long id) {
                return true;
            }
        });

        ll_empty = (LinearLayout) findViewById(R.id.ll_department_empty);
        tv_empty = (TextView) findViewById(R.id.empty_text);
        ll_empty.setVisibility(View.GONE);
        tv_empty.setText("暂无心电监测数据");
    }


    @Override
    protected void initListener() {
        iv_back.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        loading();
        String patientId = loginInfo.getPatientId();
        YiJiaHuNetWorkUtils.takePost(patientId, handler);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back://back
                finish();
                break;
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    class MyExpendAdapter extends BaseExpandableListAdapter {


        private LinkedHashMap<String, ArrayList<LinkedTreeMap<String,Object>>> data = new LinkedHashMap<>();
        private ArrayList keys = new ArrayList();

        @Override
        public void registerDataSetObserver(DataSetObserver observer) {
            super.registerDataSetObserver(observer);
        }


        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {
            super.unregisterDataSetObserver(observer);
        }

        @Override
        public int getGroupCount() {
            return data.keySet().size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return data.get(keys.get(groupPosition)).size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return keys.get(groupPosition);
        }


        @Override
        public LinkedTreeMap<String,Object> getChild(int groupPosition, int childPosition) {
            ArrayList<LinkedTreeMap<String,Object>> hdcDataDtos = data.get(keys.get(groupPosition));
            Log.i(TAG,"hdcDataDtos.size() = "+hdcDataDtos.size());
            Log.i(TAG,"groupPosition = "+groupPosition);
            Collections.sort(hdcDataDtos, new Comparator<LinkedTreeMap<String,Object>>() {

                @Override
                public int compare(LinkedTreeMap<String, Object> t1, LinkedTreeMap<String, Object> t2) {
                    if (t1.get("createTime")!=null&&t2.get("createTime")!=null){
                        String createTime1 = (String) t1.get("createTime");
                        String createTime2 = (String) t2.get("createTime");
                        return createTime2.compareTo(createTime1);
                    }
                    return -1;
                }
            });


            return hdcDataDtos.get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            View view;
            if (convertView == null) {
                view = View.inflate(mContext, R.layout.item_group_ecgfragment, null);
            } else {
                view = convertView;
            }
            TextView textview = (TextView) view.findViewById(R.id.text);
            textview.setText(getGroup(groupPosition).toString());
            return view;
        }

        @Override
        public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            View view;
            if (convertView == null) {
                view = View.inflate(mContext, R.layout.patient_ecg_list_item, null);
            } else {
                view = convertView;
            }
            TextView item_time = (TextView) view.findViewById(R.id.time);
            TextView status = (TextView) view.findViewById(R.id.status);
            final LinkedTreeMap<String,Object> hdc = getChild(groupPosition, childPosition);
            if (hdc != null) {
                String createTime = (String)hdc.get("createTime");
                if (createTime != null) {
                    item_time.setText(createTime);
                }
                String diagnose = (String) hdc.get("diagnose");
                if (diagnose!=null) {
                    status.setText("已处理");
                    status.setTextColor(Color.parseColor("#7ed321"));
                } else {
                    status.setText("未处理");
                    status.setTextColor(Color.parseColor("#ff8744"));
                }
            }
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(ReportListActivity.this, EcgraphActivtiy.class);
                    intent.putExtra(EcgraphActivtiy.ECGDATA,hdc);
                    intent.putExtra(EcgraphActivtiy.PATIENTID,loginInfo.getPatientId());
                    startActivity(intent);
                }
            });

            return view;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return false;
        }

        @Override
        public boolean areAllItemsEnabled() {
            return false;
        }

        @Override
        public boolean isEmpty() {
            return false;
        }

        @Override
        public void onGroupExpanded(int groupPosition) {

        }

        @Override
        public void onGroupCollapsed(int groupPosition) {

        }

        @Override
        public long getCombinedChildId(long groupId, long childId) {
            return 0;
        }

        @Override
        public long getCombinedGroupId(long groupId) {
            return 0;
        }

        public void setData(LinkedHashMap<String, ArrayList<LinkedTreeMap<String,Object>>> data) {
            this.data = data;
            keys = new ArrayList<>(data.keySet());
            Collections.sort(keys, new Comparator<String>() {
                @Override
                public int compare(String s, String t1) {
                    return t1.compareTo(s);
                }
            });
        }
    }


}
